package com.cyzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyzy.dao.UserRole;
import com.cyzy.mapper.UserMapper;
import com.cyzy.pojo.Role;
import com.cyzy.pojo.User;
import com.cyzy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String userAcc, String userPwd) {
//        String passwordMd5 = Md5Util.getMD5String(userPwd);
        QueryWrapper<User> yr = new QueryWrapper<>();
        yr.eq("user_acc", userAcc);
//        yr.eq("user_pwd", passwordMd5);
        yr.eq("user_pwd", userPwd);
        yr.eq("user_status", "0");
        return userMapper.selectOne(yr);
    }


    @Override
    public User selectUserByUserAccAndUserEmail(String userAcc, String userEmail) {
        QueryWrapper<User> yr = new QueryWrapper<>();
        yr.eq("user_acc", userAcc);
        yr.eq("user_email", userEmail);
        yr.eq("user_status", "0");
        return userMapper.selectOne(yr);
    }


    @Override
    public int forgotPassword(String userAcc, String userPwd) {

        UpdateWrapper<User> user = new UpdateWrapper<>();
        user.eq("user_acc", userAcc).set("user_pwd", userPwd);

        return userMapper.update(null, user);
    }

    @Override
    public int disabledUser(Integer id) {
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(User::getUserStatus, 1).eq(User::getUserId, id);
        return userMapper.update(updateWrapper);
    }

    @Override
    public Page<UserRole> selectAllUser(UserRole userRole , int pageSize, int pageNum) {
        Page<UserRole> page = new Page<>(pageNum, pageSize);
        return userMapper.selectAllUser(page , userRole.getUserId(), userRole.getUserAcc(),userRole.getUserName(),
                userRole.getUserSex(),userRole.getUserPhone(),userRole.getUserEmail(),userRole.getUserStatus(),
                userRole.getLastLoginTime(),userRole.getCreateTime(),userRole.getRoleId()
                ,userRole.getRoles());
    }

    @Override
    public List<Role> selectAllRole(Role role) {
        return userMapper.selectAllRole();
    }

    @Override
    public int updateUser(UserRole userRole){
        return userMapper.updateUser(userRole.getUserId(),userRole.getUserAcc(),userRole.getUserName(),
                userRole.getUserSex(),userRole.getUserPhone(),userRole.getUserEmail(),userRole.getRoleId(),userRole.getUserNote());
    }

    @Override
    public int updateUserStatus(UserRole userRole){
        return userMapper.updateUserStatus(userRole.getUserId(),userRole.getUserStatus());
    }

    @Override
    public int deleteUser(List<Integer> id){
        return userMapper.deleteUser(id);
    }

    @Override
    public int updateUserPwd(User user){
        return userMapper.updateUserPwd(user.getUserId(),user.getUserPwd());
    }

    @Override
    public void insertUser(UserRole userRole){
        int affectedRows = userMapper.insertUser(userRole);
        if (affectedRows > 0) {
            userMapper.addUserRole(userRole.getUserId(), userRole.getRoleIds());
        }
    }

    @Override
    public UserRole exitUser(UserRole userRole){
        return userMapper.existUser(userRole);
    }

    @Override
    public int addUserRole(UserRole userRole){
        userMapper.deleteAllUserRole(userRole.getUserId());
        return userMapper.addUserRole(userRole.getUserId(),userRole.getRoleIds());
    }

    @Override
    public List<UserRole> getUserRole(UserRole userRole){
        return userMapper.getUserRole(userRole.getUserId(),userRole.getRoleId());
    }
    @Override
    public List<UserRole> getUser(UserRole userRole){
        return userMapper.getUser(userRole);
    }

}
